package com.cqhfan.chatroom.handler;

import org.springframework.web.socket.WebSocketSession;

import java.util.Collections;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author 眭传洪
 * @create 2024-11-19 20:21
 */
public class UserConnectionSession {

    private static final Map<Long, WebSocketSession> CONNECTIONS = new ConcurrentHashMap<>();

    private static final Set<Long> CONNECTION_USER_IDS = Collections.synchronizedSet(new HashSet<>());

    public static void addUserIdSessionRelation(Long userId, WebSocketSession socketSession) {
        CONNECTIONS.put(userId, socketSession);
        CONNECTION_USER_IDS.add(userId);
    }

    public static void removeUserIdSessionRelation(Long userId) {
        CONNECTIONS.remove(userId);
        CONNECTION_USER_IDS.remove(userId);
    }

    public static WebSocketSession getSessionByUserId(Long userId) {
        return CONNECTIONS.get(userId);
    }

    public static Set<Long> getConnectionUserIds() {
        return CONNECTION_USER_IDS;
    }

}
